package com.dzl.supplier.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.ApiExtractCardConstant;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareOrderEnum;
import com.dzl.supplier.dao.WelfareSupplierMapper;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.pojo.WelfareSupplierBrand;
import com.dzl.supplier.pojo.WelfareSupplierPaymentinfo;
import com.dzl.supplier.pojo.dto.WelfareSupplierDeliveryDto;
import com.dzl.supplier.pojo.dto.WelfareSupplierDto;
import com.dzl.supplier.pojo.dto.WelfareSupplierRestrictedAreasDto;
import com.dzl.supplier.pojo.query.WelfareSupplierQuery;
import com.dzl.supplier.pojo.vo.WelfareSupplierVo;
import com.dzl.supplier.service.WelfareSupplierBrandService;
import com.dzl.supplier.service.WelfareSupplierPaymentinfoService;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.PasswordUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 供应商信息表(WelfareSupplier)表服务实现类
 * @author caobucao
 * @since 2021-05-18 16:31:01
 */
@Service("welfareSupplierService")
public class WelfareSupplierServiceImpl extends BaseServiceImpl<WelfareSupplier> implements WelfareSupplierService {

    @Autowired
    private WelfareSupplierMapper mapper;
    @Autowired
    private WelfareSupplierBrandService welfareSupplierBrandService;
    @Autowired
    private WelfareSupplierPaymentinfoService welfareSupplierPaymentinfoService;

    @Override
    public PageInfo<WelfareSupplierVo> getSupplierVoListPage(WelfareSupplierQuery query) {
        PageHelper.startPage(query);
        List<WelfareSupplierVo> list = mapper.selectSupplierVoListPage(query);
        list.forEach(x ->{
            if (EmptyUtils.isNotEmpty(x.getIsApi()) && x.getIsApi().equals(1)){
                if (EmptyUtils.isNotEmpty(x.getMerchantId()) && x.getMerchantId().equals(ApiExtractCardConstant.MERCHANT_ID)){
                    x.setApiType(WelfareOrderEnum.ORDER_API_TYPE_EXTRACT_CARD.getCode());
                } else {
                    x.setApiType(WelfareOrderEnum.ORDER_API_TYPE_RECHARGE.getCode());
                }
            }
            if(EmptyUtils.isNotEmpty(x.getPassword())){
                x.setPassword(PasswordUtil.AESDecrypt(x.getPassword()));
            }
            //限制地区
            String restrictedAreasStr = x.getRestrictedAreas();
            if(EmptyUtils.isEmpty(restrictedAreasStr)) {
                x.setRestrictedAreasStr("不限制地区");
            } else {
                x.setRestrictedAreasStr(restrictedAreasStr);
            }
            Integer holidayPostpone = x.getHolidayPostpone();
            if(holidayPostpone.equals(1)) {
                x.setDeliveryDes(x.getDeliveryDes() + "，节假日顺延");
            }
        });
        PageInfo<WelfareSupplierVo> page = new PageInfo<WelfareSupplierVo>(list);
        return page;
    }

    @Override
    public List<WelfareSupplier> selectSupplierListByBrandId(WelfareSupplierQuery query) {
        List<WelfareSupplier> list = mapper.selectSupplierListByBrandId(query);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveSupplier(WelfareSupplierDto welfareSupplier) {
        String deliveryDaysDes = "";
        if (welfareSupplier.getDeliveryDays() == 0) {
            deliveryDaysDes += "当天发货";
        } else if (welfareSupplier.getDeliveryDays() == 1) {
            deliveryDaysDes += "明日发货";
        } else {
            deliveryDaysDes += welfareSupplier.getDeliveryDays().toString() + "天内发货";
        }
        String deliveryDes = welfareSupplier.getDeliveryTime() + "前下单，" + deliveryDaysDes;
        welfareSupplier.setDeliveryDes(deliveryDes);
        if (EmptyUtils.isEmpty(welfareSupplier.getId())) {
            //查询供应商名称是否存在
            Example example= Example.builder(WelfareSupplier.class).where(Sqls.custom()
                    .andEqualTo("status", CommonConstants.YES)
                    .andEqualTo("name", welfareSupplier.getName()))
                    .build();
            int existCount = mapper.selectCountByExample(example);
            if(existCount>0){
                throw new CommonException("该名称已存在，请重新输入");
            }
            mapper.insertSelective(welfareSupplier);
            //添加打款账户信息
            if(EmptyUtils.isNotEmpty(welfareSupplier.getSupplierPaymentinfoList())){
                for (WelfareSupplierPaymentinfo welfareSupplierPaymentinfo: welfareSupplier.getSupplierPaymentinfoList()) {
                    welfareSupplierPaymentinfo.setSupplierId(welfareSupplier.getId());
                }
                welfareSupplierPaymentinfoService.insertList(welfareSupplier.getSupplierPaymentinfoList());
            }
            //添加关联品牌信息
            if(EmptyUtils.isNotEmpty(welfareSupplier.getSupplierBrandList())){
                ArrayList<WelfareSupplierBrand> welfareSupplierBrandArrayList = new ArrayList<>();
                for (WelfareBrand welfareBrand: welfareSupplier.getSupplierBrandList()) {
                    WelfareSupplierBrand welfareSupplierBrand = new WelfareSupplierBrand();
                    welfareSupplierBrand.setSupplierId(welfareSupplier.getId());
                    welfareSupplierBrand.setBrandId(welfareBrand.getId());
                    welfareSupplierBrand.setStatus(CommonConstants.YES);
                    welfareSupplierBrand.setCreateTime(new Date());
                    welfareSupplierBrandArrayList.add(welfareSupplierBrand);
                }
                welfareSupplierBrandService.insertList(welfareSupplierBrandArrayList);
            }
        } else {
            //查询供应商名称是否存在
            Example example= Example.builder(WelfareSupplier.class).where(Sqls.custom()
                    .andEqualTo("status", CommonConstants.YES)
                    .andNotEqualTo("id",welfareSupplier.getId())
                    .andEqualTo("name", welfareSupplier.getName()))
                    .build();
            int existCount = mapper.selectCountByExample(example);
            if(existCount>0){
                throw new CommonException("该名称已存在，请重新输入");
            }
            //查询删除的品牌
            WelfareSupplierBrand welfareSupplierBrandParam = new WelfareSupplierBrand();
            welfareSupplierBrandParam.setSupplierId(welfareSupplier.getId());
            welfareSupplierBrandService.delete(welfareSupplierBrandParam);
            //删除原打款信息
            WelfareSupplierPaymentinfo welfareSupplierPaymentinfoDeleteParam = new WelfareSupplierPaymentinfo();
            welfareSupplierPaymentinfoDeleteParam.setSupplierId(welfareSupplier.getId());
            welfareSupplierPaymentinfoService.delete(welfareSupplierPaymentinfoDeleteParam);
            //修改供应商信息
            if(EmptyUtils.isEmpty(welfareSupplier.getRestrictedAreas())){
                welfareSupplier.setRestrictedAreas("不限制地区");
            }
            welfareSupplier.setUpdateTime(new Date());
            mapper.updateByPrimaryKeySelective(welfareSupplier);
            //添加打款账户信息
            if(EmptyUtils.isNotEmpty(welfareSupplier.getSupplierPaymentinfoList())){
                for (WelfareSupplierPaymentinfo welfareSupplierPaymentinfo: welfareSupplier.getSupplierPaymentinfoList()) {
                    welfareSupplierPaymentinfo.setSupplierId(welfareSupplier.getId());
                }
                welfareSupplierPaymentinfoService.insertList(welfareSupplier.getSupplierPaymentinfoList());
            }
            //添加关联品牌信息
            if(EmptyUtils.isNotEmpty(welfareSupplier.getSupplierBrandList())){
                ArrayList<WelfareSupplierBrand> welfareSupplierBrandArrayList = new ArrayList<>();
                for (WelfareBrand welfareBrand: welfareSupplier.getSupplierBrandList()) {
                    WelfareSupplierBrand welfareSupplierBrand = new WelfareSupplierBrand();
                    welfareSupplierBrand.setSupplierId(welfareSupplier.getId());
                    welfareSupplierBrand.setBrandId(welfareBrand.getId());
                    welfareSupplierBrand.setStatus(CommonConstants.YES);
                    welfareSupplierBrand.setCreateTime(new Date());
                    welfareSupplierBrandArrayList.add(welfareSupplierBrand);
                }
                welfareSupplierBrandService.insertList(welfareSupplierBrandArrayList);
            }
        }
        return 1;
    }

    @Override
    public Integer selectCountRelateGoodsBySupplierId(Integer id) {
        return mapper.selectCountRelateGoodsBySupplierId(id);
    }

    @Override
    public WelfareSupplier querySupplierDetail(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer batchUpdateSupplierDeliveryInfo(WelfareSupplierDeliveryDto dto) {
        String deliveryDaysDes = "";
        if (dto.getDeliveryDays() == 0) {
            deliveryDaysDes += "当天发货";
        } else if (dto.getDeliveryDays() == 1) {
            deliveryDaysDes += "明日发货";
        } else {
            deliveryDaysDes += dto.getDeliveryDays().toString() + "天内发货";
        }
        String deliveryDes = dto.getDeliveryTime() + "前下单，" + deliveryDaysDes;
        dto.setDeliveryDes(deliveryDes);
        return mapper.batchUpdateSupplierDeliveryInfo(dto);
    }

    @Override
    public Integer batchUpdateSupplierRestrictedAreas(WelfareSupplierRestrictedAreasDto dto) {
        return mapper.batchUpdateSupplierRestrictedAreas(dto);
    }


}
